package com.redis.seckill.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.redis.seckill.entity.Product;
import com.redis.seckill.entity.User;
import com.redis.seckill.mapper.UserMapper;
import com.redis.seckill.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.redis.seckill.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @since 2020-10-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;


    @Override
    public User getOne(Integer userId) {
        if (redisUtil.hasKey("userInfo"+userId)) {
            Object o = redisUtil.get("userInfo" + userId);
            String jsonStr = JSONObject.toJSONString(o);
            return JSONObject.parseObject(jsonStr, User.class);
        }else {
            User user = userService.getOne(new QueryWrapper<User>().eq("user_id", userId));
            redisUtil.set("userInfo"+userId,JSON.toJSON(user));
            return user;
        }
    }
}
